;$(function() {
    var insightdata = ainsightdata;
    insightdata = JSON.stringify(insightdata);
    while (insightdata.indexOf("&^^~^^~^^~^^&") >=0){
        insightdata = insightdata.replace("&^^~^^~^^~^^&","<")
    }
    while (insightdata.indexOf("&@@~@@~@@~@@&") >=0){
        insightdata = insightdata.replace("&@@~@@~@@~@@&",">")
    }
    insightdata = JSON.parse(insightdata)
    var table = $('#tempo-mine-viewing-resultshow');
    $('#showName').text(insightdata.flowName+':'+insightdata.nodeName);
    $('title').text(insightdata.flowName);
    //flag判断是否为信号节点的数据
    var flag = insightdata.flag;
    var resultTableData = insightdata.resultTableData;

    var nodeConfigId = insightdata.nodeConfigId;
    var vectorUDTIndexArr = [];
    var metaNames = resultTableData[1];
    var newMetaNames = [];
    for (var i = 0 ; i < metaNames.length; i++) {
        var metaName = metaNames[i];
        // 如果在国际化文件中有此国际化项，则使用国际化项的值
        var newMetaName = TempoUtils.locale(nodeConfigId + "."+ nodeConfigId + "_" + metaName);
        // 有的节点的国际化形式是 节点名.字段名
        newMetaName = newMetaName || TempoUtils.locale(nodeConfigId + "." + metaName);
        //过滤掉属性生成字段的国际化
        if (typeof(newMetaName) == "undefined" || newMetaName=='' || nodeConfigId == "addattributes") {
            newMetaNames.push(TempoMineUtils.encodeHTML(metaName));
        } else {
            newMetaNames.push(TempoMineUtils.encodeHTML(newMetaName));
        }
    }
    var metaTypes = resultTableData[0];
    if (metaTypes && metaTypes.length && metaTypes.length > 0) {
        for (var index = 0; index < metaTypes.length; index++) {
            if (metaTypes[index].indexOf('org.apache.spark.ml.linalg.VectorUDT') != -1) {
                vectorUDTIndexArr.push(index);
            }
        }
    }
    var meta = [];
    for (var i = 0; i < resultTableData.length && i <102; i++) {
        if (i != 1) {
            var dataItem = resultTableData[i];
            if (dataItem.length > 0 && dataItem[0] != '...') {
                for (var j = 0; j < vectorUDTIndexArr.length;j++) {
                    var vectorUDTItem = dataItem[vectorUDTIndexArr[j]];
                    for (var k = 0; vectorUDTItem!=null && k < vectorUDTItem.length; k++) {
                        if (typeof vectorUDTItem[k] == 'number'){
                            vectorUDTItem[k] = parseFloat((vectorUDTItem[k]).toFixed(4));
                        }
                    }
                }
                if(vectorUDTIndexArr.length==0){
                    //數值轉化
                    for(var j = 0; j < dataItem.length;j++){
                        var numItem = dataItem[j];
                        //如果是字符型的话就不需要format
                        if ($.isNumeric(numItem) && resultTableData[0][j] != "StringType"){
                            numItem = TempoMineUtils.format(numItem);
                            dataItem[j] = TempoMineUtils.encodeHTML(numItem)
                        }
                    }
                }
            }
            meta.push(dataItem);
        }
    }
    var buildTableByMeta = function(meta, metaNames, className,titleName) {
        var tableStr = "<div style='overflow: hidden;height: 28px;'><div id='moveDiv' style='overflow-x: auto;overflow-y: hidden;height: 47px;margin-right: 8px;'><div id='tableThead' ><table class='" + className + "'>";
        // 添加表头信息
        if (metaNames && metaNames.length > 0) {
            tableStr += "<thead>";
            if (titleName && titleName != "") {
                tableStr += "<tr>";
                tableStr += "<th colspan='" + metaNames.length
                    + "' style='text-align:center'>" + titleName + "</th>";
                tableStr += "</tr>";
            }
            tableStr += "<tr>";
            // 先添加表头信息
            for (var i = 0; i < metaNames.length; i++) {
//				tableStr += "<th style='min-width:100px;'>" + metaNames[i] + "</th>";
                tableStr += "<th>" + metaNames[i] + "</th>";
            }
            tableStr += "</tr></thead>";
        }
        debugger
        if (meta && meta.length > 0) {
            tableStr += "<tbody style='visibility: collapse;'>";
            // 取出列类型，并转换
            var typeFlag = TempoMineUtils.validIsNumberType(meta[0]);
            //根据列的个数来确定需要限制的个数，默认按照10列来计算，每列占20个字符
            var contentLimit = metaNames.length >= 10 ? 20 : (10 - metaNames.length) * 20;
            // 添加数据
            for (var i = 1; i < 20; i++) {
                if (meta[i]) {
                    tableStr += "<tr>";
                    for (var j = 0; j < meta[i].length; j++) {
                        if (meta[i][j] == undefined){
                            tableStr += "<td></td>";
                            continue;
                        }
                        // 小数位数处理
                        if (typeFlag[j]){
                            tableStr += "<td>"+TempoMineUtils.format(meta[i][j])+"</td>";
                        } else {
                            var infoStr = meta[i][j];
                            if($.isPlainObject(meta[i][j]) || $.isArray(meta[i][j])){
                                infoStr = JSON.stringify(meta[i][j]);
                            }
                            if (infoStr && infoStr.length > contentLimit && flag != window.mineConstant.PORT_SIGNAL) {
                                // 最多显示20个字符<contentLimit>
                                var tip = infoStr;
                                infoStr = TempoMineUtils.encodeHTML(infoStr.substring(0, contentLimit));
                                tip = TempoMineUtils.encodeHTML(tip.substring(0, 200));
                                tableStr += "<td title=\""+ tip +"\">" + infoStr + "...</td>";
                            } else if (infoStr && infoStr.length > contentLimit && flag == window.mineConstant.PORT_SIGNAL && j == 0){//信号处理 文件名显示通道+文件名称
                                infoStr = TempoMineUtils.encodeHTML(infoStr.substring(infoStr.lastIndexOf("/")+1));
                                tableStr += "<td>" + infoStr + "</td>";
                            } else if (infoStr && infoStr.length > contentLimit && flag == window.mineConstant.PORT_SIGNAL && j == 3){//信号处理 信号数据展示前5个数据 悬浮100个
                                var showData = "[";
                                var tip = "[";
                                var arrayData = infoStr.substring(1, infoStr.length - 1).split(",");
                                for(var k = 0;k < 5;k++){
                                    showData = showData + arrayData[k] + ",";
                                }
                                var tipLength = 100;
                                if(arrayData.length < 100){
                                    tipLength = arrayData.length;
                                }
                                for(var k = 0;k < tipLength;k++){
                                    if(k == 99){
                                        tip= tip + arrayData[k] + "]";
                                    }else{
                                        tip= tip + arrayData[k] + ",";
                                    }
                                }
                                tableStr += "<td title=\""+ tip +"\">" + showData + "...]</td>";
                            } else {
                                infoStr = TempoMineUtils.encodeHTML(infoStr);
                                tableStr += "<td>" + infoStr + "</td>";
                            }
                        }
                    }
                    tableStr += "</tr>";
                }
            }
            tableStr += "</tbody></table></div></div></div>";
        }
        tableStr += "<div id='tableTbody' style='height: calc(100% - 80px);overflow: auto;'><table class='" + className + "'>";
        // 添加表头信息
        if (metaNames && metaNames.length > 0) {
            tableStr += "<thead style='display: table-footer-group;visibility:collapse;'>";
            if (titleName && titleName != "") {
                tableStr += "<tr>";
                tableStr += "<th colspan='" + metaNames.length
                    + "' style='text-align:center'>" + titleName + "</th>";
                tableStr += "</tr>";
            }
            tableStr += "<tr>";
            // 先添加表头信息
            for (var i = 0; i < metaNames.length; i++) {
//				tableStr += "<th style='min-width:100px;'>" + metaNames[i] + "</th>";
                tableStr += "<th>" + metaNames[i] + "</th>";
            }
            tableStr += "</tr></thead>";
        }
        // 添加表内容信息
        if (meta && meta.length > 0) {
            tableStr += "<tbody>";
            // 取出列类型，并转换
            var typeFlag = TempoMineUtils.validIsNumberType(meta[0]);
            //根据列的个数来确定需要限制的个数，默认按照10列来计算，每列占20个字符
            var contentLimit = metaNames.length >= 10 ? 20 : (10 - metaNames.length) * 20;
            // 添加数据
            for (var i = 1; i < meta.length; i++) {
                if (meta[i]) {
                    tableStr += "<tr>";
                    for (var j = 0; j < meta[i].length; j++) {
                        if (meta[i][j] == undefined){
                            tableStr += "<td></td>";
                            continue;
                        }
                        // 小数位数处理
                        if (typeFlag[j]){
                            tableStr += "<td>"+TempoMineUtils.format(meta[i][j])+"</td>";
                        } else {
                            var infoStr = meta[i][j];
                            if($.isPlainObject(meta[i][j]) || $.isArray(meta[i][j])){
                                infoStr = JSON.stringify(meta[i][j]);
                            }
                            if (infoStr && infoStr.length > contentLimit && flag != window.mineConstant.PORT_SIGNAL){
                                // 最多显示20个字符<contentLimit>
                                var tip = infoStr;
                                infoStr = TempoMineUtils.encodeHTML(infoStr.substring(0, contentLimit));
                                tip = TempoMineUtils.encodeHTML(tip.substring(0, 200));
                                tableStr += "<td title=\""+ tip +"\">" + infoStr + "...</td>";
                            }else if(infoStr && infoStr.length > contentLimit && flag == window.mineConstant.PORT_SIGNAL && j == 0){//信号处理 文件名显示通道+文件名称
                                infoStr = TempoMineUtils.encodeHTML(infoStr.substring(infoStr.lastIndexOf("/")+1));
                                tableStr += "<td>" + infoStr + "</td>";
                            }else if(infoStr && infoStr.length > contentLimit && flag == window.mineConstant.PORT_SIGNAL && j == 3){//信号处理 信号数据展示前5个数据 悬浮100个
                                var showData = "[";
                                var tip = "[";
                                var arrayData = infoStr.substring(1, infoStr.length - 1).split(",");
                                for(var k = 0;k < 5;k++){
                                    showData = showData + arrayData[k] + ",";
                                }
                                var tipLength = 100;
                                if(arrayData.length < 100){
                                    tipLength = arrayData.length;
                                }
                                for(var k = 0;k < tipLength;k++){
                                    if(k == 99){
                                        tip = tip + arrayData[k] + "]";
                                    }else{
                                        tip = tip + arrayData[k] + ",";
                                    }

                                }
                                tableStr += "<td title=\""+ tip +"\">" + showData + "...]</td>";
                            } else {
                                infoStr = TempoMineUtils.encodeHTML(infoStr);
                                tableStr += "<td>" + infoStr + "</td>";
                            }
                        }
                    }
                    tableStr += "</tr>";
                }
            }
            tableStr += "</tbody>";
        }
        tableStr += "</table></div>";
        return $(tableStr);
    };
    //处理
    var resTable = buildTableByMeta(meta, newMetaNames, "tempo-mine-insight-table","");
    resTable.appendTo(table);
    $('#download').click(function(){
        var data = TempoUtils.clone(insightdata.resultTableData)
        if(data.length > 102){
            data = data.splice(1,101)
        }else{
            data.splice(0,1);
        }
        if(flag == window.mineConstant.PORT_SIGNAL){
            for(var i = 1;i < data.length;i++){
                var fileName = data[i][0].substring(data[i][0].lastIndexOf("/") + 1);
                data[i][0] = fileName;
                var newSignalData = "[";
                var signalData = data[i][3].substring(1,data[i][3].length-1).split(",");
                var dataLength = 100;
                if(signalData.length < 100){
                    dataLength = signalData.length;
                }
                for(var j = 0;j < dataLength;j++){
                    if(j == dataLength - 1){
                        newSignalData = newSignalData + signalData[j] + "]";
                    }else{
                        newSignalData = newSignalData + signalData[j] + ",";
                    }
                }
                data[i][3] = newSignalData;
            }
        }
        var tableData = {
            "matrixdatas":JSON.stringify(data),
            "fileName":"data.xls"
        };
        TempoUtils.downLoadFile({
            url: contextPath + "/api/ai/mineresource/downloadTables",
            data :tableData
        });
    });
    $('#tableTbody').scroll(function(){
        var left =  $(this).scrollLeft();
        $('#moveDiv').animate({scrollLeft:left}, 1);
    });
    debugger
    if($('#tableTbody')[0].offsetHeight >= $('#tableTbody')[0].scrollHeight ){
        $('#moveDiv').css('margin-right', '0px')
        if(TempoUtils.isIE() || TempoUtils.isFirefox()){
            $('#tableTbody').css('width', 'calc(100%)');
        }

    }else {
        if(TempoUtils.isIE() || TempoUtils.isFirefox()){
            $('#tableTbody').css('width', 'calc(100% + 9px)');
        }
    }

})